package org.madmac.join;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.wakeful.conn.Connection;


public class CountDao {

	private static CountDao instance;

	private CountDao() {
	}

	public static CountDao getInstance() {

		if (instance == null) {
			instance = new CountDao();
		}
		return instance;
	}

	public int countIp() throws SQLException, ClassNotFoundException, Exception {

		java.sql.Connection conn = Connection.getInstance();
		Statement stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS ip FROM madmac.ip ";

		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			return 0;
		}

		int max_ip = 0;

		if (rs.next()) {

			max_ip = rs.getInt("ip");
		}
		return max_ip;
	}

	public int countSalas() throws SQLException, ClassNotFoundException, Exception {

		int max_sala = 0;
		java.sql.Connection conn = Connection.getInstance();
		Statement stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS sala FROM madmac.setor ";

		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			return 0;
		}

		if (rs.next()) {
			max_sala = rs.getInt("sala");
		}
		return max_sala;
	}

	public int countMaquinas() throws SQLException, ClassNotFoundException, Exception {

		int max_maquina = 0;
		java.sql.Connection conn = Connection.getInstance();
		Statement stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS maquina FROM madmac.maquina ";

		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			return 0;
		}

		if (rs.next()) {
			max_maquina = rs.getInt("maquina");
		}
		return max_maquina;
	}

	public int countIpLivre(int faixa_ip) throws SQLException, ClassNotFoundException, Exception {

		int ip_livre = 0;
		java.sql.Connection conn = Connection.getInstance();
		Statement stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS ip_livre FROM madmac.ip WHERE livre = 'true' ";
		sql += "AND id_faixa_ip = " + faixa_ip + " ";

		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			return 0;
		}

		if (rs.next()) {
			ip_livre = rs.getInt("ip_livre");
		}
		return ip_livre;
	}

	public int countMAC() throws SQLException, ClassNotFoundException, Exception {

		int mac = 0;
		java.sql.Connection conn = Connection.getInstance();
		Statement stmt = conn.createStatement();
		String sql = "SELECT COUNT(*) AS mac FROM madmac.placa_rede ";

		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			return 0;
		}
		if (rs.next()) {
			mac = rs.getInt("mac");
		}
		return mac;
	}
}
